Uploading files on livecode web server
In this example we look at how to use LiveCode server to upload a file. You'll need web server with LiveCode Server in order to follow along with this lesson. If you need hosting, http://www.on-rev.com is provided by Livecode Ltd and is pre configured with the latest LiveCode Server engines. Create the basic HTML form We need to start by creating a web form that lets the user select a file form their computer and 'post' this information to the server. This is form.html: Upload Form Choose a file to upload: A few key things to point out: * enctype = "multipart/form-data": This tells the browser that the form is going to pass multipart data which is what we have to set if we're going to upload files. * action = "file_upload_example.lc": You'll see in the example that the action is a LiveCode file. This is where the form will post its data too and where our script to handle the upload should go. We still need to write it, see below. * name="uploadedfile": You'll notice that we've created a 'input' form element with the name 'uploadedfile'. The file data will appear in the $_Files array under that key. Create the livecode script Add the LiveCode Script to check for the uploaded file, this is file_upload_example.lc: Upload Form Choose a file to upload: Uploaded Files " else put $_Filesuploadedfilename into tFileName put $_Filesuploadedfiletype into tFileType put $_Filesuploadedfilesize into tFileSize put $_Filesuploadedfilefilename into tFilePath put "Your file '" & tFileName & "' was uploaded successfully. It is" && tFileSize && "bytes in size." end if ?> # Upload it to your server. # Open a browser and navigate to your file. # Click 'choose file' and select any file from your local system. # Wait for it to upload. #It should display the name and size of the uploaded file. You'll notice we've added a little extra HTML header 'Uploaded Files' under which we are displaying the information from our upload ($_FILES). Notice that all the information, including errors are stored in the $_FILES array against a key with the same name as our input box (uploadedfile) in the web form. You can access the name, type, size and path of the uploaded file via the $_FILES array. Do something with the file In this step I've added code which displays the content of the file if it is found to be a 'text' file. This information is available in the $_FILES array. This is file_upload_example.lc (advanced version): Upload Form Choose a file to upload: Uploaded Files " else put $_Filesuploadedfilename into tFileName put $_Filesuploadedfiletype into tFileType put $_Filesuploadedfilesize into tFileSize put $_Filesuploadedfilefilename into tFilePath put "Your file '" & tFileName & "' was uploaded successfully. It is" && tFileSize && "bytes in size." // If the file is a text file, the display what is in the file if tFileType is "text/plain" then put URL ("file:" & tFilePath) into tText put " File Content " & tText & " " end if end if ?> Category:Server